/**
 * @fileOverview View modul of Counter widget MVC
 * 
 * @author Adam Hořčica horcicaa@gmail.com
 * @url $HeadURL: http://ahs-gadgets.googlecode.com/svn/trunk/bb-counters/src/counter_view.js $
 * @revision $Id: counter_view.js 47 2008-02-09 20:30:13Z horcicaa $
 */

/**
 * View modul of Counter widget MVC
 *  
 * @constructor
 * @param {CounterController} controller Controller of widget
 * @param {string} name Default name of counter (title of widget)
 * @param {boolean} state State of detail
 * @param {number} n Counter index (position in list) 
 */ 
function CounterView(controller, name, state, n)
{
  /** Widget controller */
  this.controller = controller;
  
  /** Default name */
  this.name = name;
  /** Default state */
  this.state = state;
  /** Indexo of counter in list */
  this.index = n;
  
  /** Important DOM element of widget GUI */
  this.DOM = {};
}

CounterView.prototype = {

  /**
   * Generate DOM structure of widget GUI
   * 
   * @return {DOMElement} Widget DOM structure      
   */     
  generateDOM: function()
  {
    var that = this;
    var DOM = this.DOM;
    
    DOM.item = $.c("div", this.getClassName());
    
    var head = $.c("div", "head");
    
    var icon = $.c("a", "icon", "&nbsp;");
    icon.href="javascript:void(0);";
    
    DOM.title = $.c("strong", null);
    
    /*
    DOM.link = $.c("a", null, this.name);
    DOM.link.target = "_blank";
    DOM.link.href = "javascript:;";
    */
    
    DOM.total = $.c("var", null, "?");
    DOM.total.title = "__MSG_total_title__";
    
    $.a(head, icon);
    //$.a(DOM.title, DOM.link);
    $.a(head, DOM.title);          
    $.a(head, DOM.total);
    
    $.a(DOM.item, head);
    var info = $.c("div", "info");
    
    /** @ignore */
    function getInfoLine(id, title)
    {
      var line = $.c("div", "line " + id);
      $.a(line, $.c("em", null, title));
      
      var values = $.c("var");
      DOM[id] = {};
      DOM[id].visits = $.c("span", null, "?");
      DOM[id].pageviews = $.c("span", null, "?");
      DOM[id].visits.title = "__MSG_visits_title__";
      DOM[id].pageviews.title = "__MSG_previews_title__";
      $.a(values, DOM[id].visits);
      $.a(values, $.txt(" / "));
      $.a(values, DOM[id].pageviews);
      return $.a(line, values);
    }
    
    $.a(info, getInfoLine("today", "__MSG_today_label__"));
    $.a(info, getInfoLine("yesterday", "__MSG_yesterday_label__"));
    $.a(info, getInfoLine("month", "__MSG_month_label__"));
    
    $.a(DOM.item, info);
    
    icon.onclick = function()
    {
      that.controller.changeState(!that.state);
    };
    
    return DOM.item;
  },
  
  /**
   * Set state of detail (true for open)
   * @param {boolean} State of detail   
   */     
  setState: function(state)
  {
    this.state = state;
    this.DOM.item.className = this.getClassName();
  },
  
  /**
   * Set counter data
   * @param {data_object} data Counter data    
   */     
  setData: function(data)
  {
    var DOM = this.DOM;
    
    /*
    DOM.link.innerHTML = data.name;
    DOM.link.href = data.link;
    */
    var title = $TPL("__MSG_counter_name_link_title__", {id: data.id});
    
    DOM.title.innerHTML = "<a href='" + data.link + "' target='_blanc' title='" + title + "'>" + data.name + "</a>";
    DOM.total.innerHTML = CounterView.formatNumber(data.total);
    
    /** @ignore */
    function setInfo(item)
    {
      DOM[item].visits.innerHTML = CounterView.formatNumber(data[item].visits);
      DOM[item].pageviews.innerHTML = CounterView.formatNumber(data[item].pageviews);
    }
    
    setInfo("today");
    setInfo("yesterday");
    setInfo("month");
  },
  
  /**
   * Set information label (in head of widget)
   * @param {string} txt Label text   
   */     
  setLabel: function(txt)
  { 
    this.DOM.title.innerHTML = "<span>" + txt + "</span>";
  },
  
  getClassName: function()
  {
    return "item " + (this.state?"open":"close") + " " + (this.index%2==0?"even":"odd") + (this.index==0?" first":""); 
  }

};

/**
 * Separate orders of magnitude by space
 *
 * @example 12345678 -> "12 345 678"
 * @static
 * @param {number} number Number for formating
 * @return {string} Formated number   
 */ 
CounterView.formatNumber = function(number)
{
  var i, num = number.toString(), temp = [];
  for(i = num.length; i >= 3; i-= 3) temp.push(num.substr(i-3, 3));
  temp.push(num.substr(0,i));
  return temp.reverse().join(" ");
}

